import ash from '@ash.ts/ash';
import { GunControlNode } from '../data/nodes';
import { KeyPoll } from '../input/KeyPoll';
import { EntityCreator } from '../util/EntityCreator';

export class GunControlSystem extends ash.ListIteratingSystem<GunControlNode> {
    private keyPoll: KeyPoll;

    private creator: EntityCreator;

    public constructor(keyPoll: KeyPoll, creator: EntityCreator) {
        super(GunControlNode);
        this.keyPoll = keyPoll;
        this.creator = creator;
    }

    public updateNode(node: GunControlNode, time: number): void {
        const { control } = node;
        const { position } = node;
        const { gun } = node;

        gun.shooting = this.keyPoll.isDown(control.trigger);
        gun.timeSinceLastShot += time;
        if (gun.shooting && gun.timeSinceLastShot >= gun.minimumShotInterval) {
            this.creator.createUserBullet(gun, position);
            gun.timeSinceLastShot = 0;
        }
    }
}
